<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../jquery-3.3.1.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .body {
            margin-top: 20px;
            width: 80%;
            height: 2000px;
            margin: 0 auto;
            background-color: blanchedalmond;
        }
        
        .wai {
            margin-top: 20px;
            width: 600px;
            height: 400px;
            margin: 0 auto;
            border: 5px solid red;
            position: relative;
        }
        
        #dv {
            width: 40px;
            height: 40px;
            border: 5px solid green;
            position: absolute;
        }
        
        #top {
            width: 80px;
            height: 80px;
            background-color: red;
            position: fixed;
            _position: absolute;
            right: 60px;
            top: 0px;
            text-align: center;
            line-height: 80px;
            font-size: 20px;
            display: none;
        }
    </style>
</head>

<body>
    <div id="top">
        <span>返回顶部</span>
    </div>
    <div class="wai">
        <div id="dv"></div>
    </div>
    <div class="body"></div>
    <script>
        /** var dv = document.getElementById('dv');
                      var x = 0;
                      var y = 0;
                      var l = 0;
                      var t = 0;
                      var isD = false;
                      dv.onmousedown = function(e) {
                          x = e.clientX;
                          y = e.clientY;
                          l = dv.offsetLeft;
                          t = dv.offsetTop;
                          isD = true;
                          $(dv).css("cursor", "move");
                      }
                      window.onmousemove = function(e) {
                      if (isD == false) {
                          return;
                      }
                      var nx = e.clientX;
                      var ny = e.clientY;
                      var nl = nx - (x - l);
                      if (nl < 0) {
                          nl = 0;
                      }
                      if (nl >= 550) {
                          nl = 550;
                      }
                      var nt = ny - (y - t);
                          if (nt < 0) {
                              nt = 0;
                          }
                          if (nt >= 350) {
                              nt = 350;
                          }
                          dv.style.left = nl + 'px';
                          dv.style.top = nt + 'px';
                      }
                      dv.onmouseup = function() {
                          isD = false;
                          dv.style.cursor = 'default';
                                                                }
                                                                **/
        $("#dv").mousedown(function(e) {
            var x = e.clientX - $("#dv").position().left;
            var y = e.clientY - $("#dv").position().top;
            $(document).mousemove(function(e) {
                var nx = e.clientX - x;
                var ny = e.clientY - y;
                if (nx < 0) {
                    nx = 0;
                }

                if (nx >= $(".wai").width() - $("#dv").outerWidth()) {
                    nx = $(".wai").width() - $("#dv").outerWidth();
                }
                if (ny < 0) {
                    ny = 0;
                }
                if (ny >= $(".wai").height() - $("#dv").outerHeight()) {
                    ny = $(".wai").height() - $("#dv").outerHeight();
                }

                $("#dv").css("left", nx + "px");
                $("#dv").css("top", ny + "px");
            })
            $(document).mouseup(function(e) {
                $(this).unbind()
            })
        })
        var t = document.documentElement.scrollTop || document.body.scrollTop;
        var bheight = document.documentElement.clientHeight || document.body.clientHeight;
        window.onscroll = window.onresize = window.onload = function() {
            $("#top").css("top", (bheight - 80) / 2 + "px");;
            t = document.documentElement.scrollTop || document.body.scrollTop;
            if (t > 400) {
                $("#top").css("display", "block");
            } else {
                $("#top").css("display", "none");
            }
        }
        $("#top").click(function() {
            $("html,body").animate({
                scrollTop: 0
            }, 500);
            //var id = setInterval("move", 100);
            //if (t == 800) {
            //    clearInterval(id);
            //}
        });
    </script>
</body>

</html>